package br.com.mmm.javacenter.framework.filters;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import br.com.mmm.javacenter.framework.filters.wrapper.HttpServletResponseWrapper;
import br.com.mmm.javacenter.framework.formatters.DateFormat;
import br.com.mmm.javacenter.framework.view.ContextAttributes;

public class FrameworkFilter implements Filter {

	public void destroy() {

		
	}

	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		response.setBufferSize(1024*16);
		HttpServletRequestWrapper httpServletRequestWrapper = new HttpServletRequestWrapper((HttpServletRequest) request){
			@Override
			public String getRemoteUser() {
				return "Marcelo Ricardo Bueno de Campos";
			}
		};
		HttpServletResponseWrapper httpServletResponseWrapper = new HttpServletResponseWrapper(response);
		httpServletRequestWrapper.setAttribute("dateTimeServer", DateFormat.simpleDate.format(new Date()));
		httpServletRequestWrapper.setAttribute(ContextAttributes.REQUEST_CONTEXT_PATH, httpServletRequestWrapper.getContextPath());
		chain.doFilter(httpServletRequestWrapper, httpServletResponseWrapper);
		response.getOutputStream().write(httpServletResponseWrapper.getBaos().getBaos().toByteArray());
	}
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}
}
